package com.ztmg.water.service.order.impl;

import com.ztmg.common.util.DateUtil;
import com.ztmg.water.mapper.student.StudentMapper;
import com.ztmg.water.model.order.OrderWaterTick;
import com.ztmg.water.model.student.Student;
import com.ztmg.water.util.RandomUtil;
import com.ztmg.water.vo.order.OrderWaterTickVO;
import com.ztmg.water.dto.order.OrderWaterTickDTO;
import com.ztmg.water.dto.order.OrderWaterTickPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.order.OrderWaterTickService;
import com.ztmg.water.mapper.order.OrderWaterTickMapper;import com.ztmg.water.model.order.OrderWaterTickExample;

@Service
public class OrderWaterTickServiceImpl implements OrderWaterTickService{

    @Resource
    private OrderWaterTickMapper orderWaterTickMapper;
    @Resource
    private StudentMapper studentMapper;
    @Override
    public JsonResult create(OrderWaterTickDTO dto) {
        if(null != dto){

            Student student = studentMapper.selectByPrimaryKey(dto.getStudentId());
            OrderWaterTick bean = new OrderWaterTick();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            bean.setOrderSn(this.getOrderSn());
            bean.setCreateTime(new Date());
            bean.setStudentName(student.getStudentName());
            bean.setStudentAdder(student.getStudentAdder());
            bean.setPlaceOrderTime(DateUtil.dateFormat(DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS),DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
            bean.setStatus(1);
            orderWaterTickMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(OrderWaterTickDTO dto) {
        if(null != dto && null != dto.getId()){
            OrderWaterTick bean = new OrderWaterTick();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            orderWaterTickMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<OrderWaterTickVO> selectById(Integer id) {
        if(null != id){
            OrderWaterTick bean = orderWaterTickMapper.selectByPrimaryKey(id);
            OrderWaterTickVO vo = new OrderWaterTickVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<OrderWaterTickVO> select(OrderWaterTickDTO dto) {
        OrderWaterTickExample example = new OrderWaterTickExample();
        OrderWaterTickExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<OrderWaterTick> sysUserList = orderWaterTickMapper.selectByExample(example);
        OrderWaterTickVO vo = new OrderWaterTickVO();
        if(!sysUserList.isEmpty()){
            BeanUtils.copyProperties(sysUserList.get(0),vo);
        }
        return JsonResult.success(vo);
    }

    @Override
    public JsonResult<PageInfo<OrderWaterTickVO>> selectPage(OrderWaterTickPageDTO dto) {
        OrderWaterTickExample example = new OrderWaterTickExample();
        OrderWaterTickExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<OrderWaterTick> beanList = orderWaterTickMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<OrderWaterTickVO> respDTOS = beanList.parallelStream().map((bean) -> {
            OrderWaterTickVO vo = new OrderWaterTickVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( orderWaterTickMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(OrderWaterTickDTO dto) {
        OrderWaterTickExample example = new OrderWaterTickExample();
        OrderWaterTickExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(orderWaterTickMapper.deleteByExample(example));
    }
    private String getOrderSn(){
        String dateStr = DateUtil.parseDateToStr(new Date(),DateUtil.DATE_TIME_FORMAT_YYYYMMDDHHMISS);
        return dateStr+ RandomUtil.getRandom(4);
    }
}